package com.itheima.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * @ClassName: MyBatisConfig
 * @Description:
 * @Author liulihui
 * @Date: 2021/7/31 17:32
 * @URL：
 * @Version 1.0
 */
public class MyBatisConfig {

    @Bean //@Bean一般不起名字.不起名字他的名字默认是方法名
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //'''''''
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.domain");


        return sqlSessionFactoryBean;
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){

        MapperScannerConfigurer Configurer = new MapperScannerConfigurer();
//配置项 就是告诉spring框架 我们的xxxDao接口在哪个包下
        Configurer.setBasePackage("com.itheima.dao");
        //xxxDao接口----->xxxDao对象  必然调用 sqlSessionFactory.openSession().getMapper(xxx接口.class);
      Configurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
      return Configurer;
    }
}